﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class staffpages_Result2 : System.Web.UI.Page
{
    XmlDocument docResult = new XmlDocument();
    XmlDocument docSurvey = new XmlDocument();
    protected void Page_Load(object sender, EventArgs e)
    {
        bindDataFromXml();
        showResult();
    }

    public void bindDataFromXml()
    {
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(Server.MapPath("~/xml/survey2.xml"));
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
        }
    }

    public void showResult()
    {
        docResult.Load(Server.MapPath("~/xml/result2.xml"));
        docSurvey.Load(Server.MapPath("~/xml/survey2.xml"));

        XmlNodeList xmlNL = docResult.GetElementsByTagName("user");
        XmlNodeList xmlNL2 = docSurvey.GetElementsByTagName("question");

        int userCount = xmlNL.Count;
        int questionCount = xmlNL2.Count;

        int[,] countResult = new int[questionCount, 4];

        string[] result;
        int c;

        for (int i = 0; i < userCount; i++)
        {
            c = i + 1;
            XmlNode nodeResult = docResult.SelectSingleNode("result/user[@userID = '" + c + "']");

            for (int j = 0; j < questionCount; j++)
            {
                result = nodeResult.ChildNodes[j].InnerText.Split(',');
                for (int u = 0; u < result.Length; u++)
                {
                    switch (result[u])
                    {
                        case "0":
                            countResult[j, 0] += 1;
                            break;
                        case "1":
                            countResult[j, 1] += 1;
                            break;
                        case "2":
                            countResult[j, 2] += 1;
                            break;
                        case "3":
                            countResult[j, 3] += 1;
                            break;
                        case "":
                            break;
                    }
                }
            }
        }

        int index = 0;
        foreach (RepeaterItem item in Repeater1.Items)
        {
            for (int j = 0; j < 4; j++)
            {
                string control = "lblAns" + (j + 1).ToString();
                Label lblAns = (Label)item.FindControl(control);
                string s = countResult[index, j].ToString() + " / " + userCount;
                lblAns.Text = s;
            }
            index++;
        }


    }
}